<?PHP
/**
 * User: Paulo Fournier
 * Date: 10-08-2013
 * Type: application/json
 * Used to retrieve the list of driver in a JSON document
 */

require_once '../../../../php/settings/config.php';
require_once '../../../../php/lib/user.php';

session_start();

$success = FALSE;   //used to check if the process was finished successfully
$errorCode = NULL;  //in case of error, will contain the code error that will be return to the client

$user = unserialize($_SESSION["user"]);

//init connection
$ch = curl_init(CRUD_URL . "/driversForUserId/" . $user->userId);
//set connection paramaters
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "charset: UTF-8"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, FALSE);
//get response
$rst = curl_exec($ch);

//request failure
if ($rst === false) {
    $err = curl_error($ch);
    $errorCode = 2;

    error_log("request: NOK");
    error_log('Curl error: ' . $err);
} //request success
else {

    $response = json_decode($rst, TRUE);

    //auth successfully
    if ($response['success']) {

        /***
         * Used to remove all zeros on the left position
         * @param $value The target value
         * @return mixed The value without zeros on the left
         */
        function removeLeftZeros($value){

            //remove first zero
            while(substr($value,0,1) === "0"){

                $value = substr($value,1,(strlen($value)-1));
            }

            return $value;
        }

        $mobile = unserialize($_SESSION["mobile"]);

        $success = TRUE;

        /*
            #driver data example
            {
               "idDriver":48477,
               "internalNumber":null,
               "name":"Nome de teste",
               "tag":"tag#1;tag#2",
               "cardNumber":"0000001105790",
               "position":1,
               "state":3,
               "vehicle":41493,
               "lastDailyRest":1376936819000,
               "lastWeeklyRest":1376738040000,
               "reducedNumberOfRests":0,
               "numberOfExtendedRestPeriods":0,
               "drivingTimeCurrentJourney":10140,
               "drivingTimeCurrentWeek":26760,
               "drivingTimeCurrentForthnight":188880
            }
         */

        $retVal = $response["retVal"];

        foreach($retVal as $i => $drv){

            $drivingLeft = ($drv["numberOfExtendedRestPeriods"] < 2 ? MAX_JOURNEY_DRIVER_10H : MAX_JOURNEY_DRIVER_9H) - $drv["drivingTimeCurrentJourney"];

            $retVal[$i] = array_merge($drv, array(
                "cardNumber" => removeLeftZeros($drv["cardNumber"]),
                "drivingLeft" => $drivingLeft,
                "mobileReference" => ($drv["vehicle"] == -1 || !isset($mobile[$drv["vehicle"]])) ? null : $mobile[$drv["vehicle"]]["reference"]
            ));

        }

        echo json_encode($retVal);

    }
    //auth un-successfully
    else {
        $errorCode = 3;
    }

}